package cn.ycgogo.org.basic.servie.impl;

import cn.ycgogo.org.basic.mapper.BaseMapper;
import cn.ycgogo.org.basic.query.BaseQuery;
import cn.ycgogo.org.basic.servie.BaseService;
import cn.ycgogo.org.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Base抽取,公共抽取方法
 *
 * @param <T> the type parameter
 * @author Yang
 * @date 2023 /1/14 14:10
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Transactional
    @Override
    public void save(T t) {
        mapper.save(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Transactional
    @Override
    public void remove(Long id) {
        System.out.println("-----------------------");
        mapper.remove(id);
    }

    @Override
    public T loadById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //个数为0返回个空数据载体给前端
        if (mapper.queryCount(query) == 0) {
            return new PageList<>();
        }
        //个数不等于0将数据返回给前端
        return new PageList<>(mapper.queryData(query),mapper.queryCount(query));
    }

    @Override
    public void patchDelete(List<Long> ids) {
        mapper.patchDele(ids);
    }


}
